package com.websocket.demo.handshakehandler;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeFailureException;
import org.springframework.web.socket.server.HandshakeHandler;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.Map;

/**
 * @author 罗俊华
 * @date 2021/8/13 - 4:48 下午
 */
public class MyHandshakeHandler implements HandshakeHandler {
    @Override
    public boolean doHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws HandshakeFailureException {

        if (request instanceof ServletServerHttpRequest) {
            ServletServerHttpRequest req = (ServletServerHttpRequest) request;
        }

        if (response instanceof ServletServerHttpResponse) {
            ServletServerHttpResponse resp = (ServletServerHttpResponse) response;
            try (ServletOutputStream out = resp.getServletResponse().getOutputStream()) {

//                out.print("hello,how are you");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        return true;
    }
}
